package org.example.module_teacher.config;

import org.example.module_teacher.utlis.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebConfig
 *
 * @author HAJ
 * @since 2025-03-03 15:14:34
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 拦截器
     */
    @Autowired
    private JwtInterceptor jwtInterceptor;

    /**
     * 拦截器 规则配置
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**") // 拦截地址
                .excludePathPatterns(); // 放行地址
    }

    /**
     * Cors 跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173") // 允许的前端域名
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true); // 允许携带 Cookie
    }
}
